package com.tdam2013.grupo04;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tdam2013.grupo04.datos.DatabaseHelper;
import com.tdam2013.grupo04.modelo.Contacto;

public class RegistrarUsuarioActivity extends Activity {

	/**
	 * sub-class of AsyncTask
	 */
	protected class RegistrarUsuarioAsyncTask extends AsyncTask<String, Integer, Boolean> {

		ProgressDialog pDialog;
		String userName;

		@Override
		protected Boolean doInBackground(String... params) {
			this.userName = params[0];
			// return MessagerManager.registrarUsuarioWeb(this.userName, "");
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			this.pDialog.dismiss();

			if (result) {
				// registro en bd
				RegistrarUsuarioActivity.this.dbHelper = new DatabaseHelper(RegistrarUsuarioActivity.this.getApplicationContext());
				result = RegistrarUsuarioActivity.this.dbHelper.addUsuario(this.userName, null, null);
				Toast.makeText(RegistrarUsuarioActivity.this.getApplicationContext(), "Usuario registrado", Toast.LENGTH_LONG).show();
				// } else {
				// if (MessagerManager.getMensajeResultado() == XmlResultHandler.RESULT_USUARIO_EXISTENTE) {
				// Toast.makeText(RegistrarUsuarioActivity.this.getApplicationContext(), "El usuario ya existe", Toast.LENGTH_LONG).show();
				// } else {
				// Toast.makeText(RegistrarUsuarioActivity.this.getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
				// }
			}
		}

		@Override
		protected void onPreExecute() {
			this.pDialog = new ProgressDialog(RegistrarUsuarioActivity.this);
			this.pDialog.setMessage(RegistrarUsuarioActivity.this.getString(R.string.esperePorFavor));
			this.pDialog.show();
		};
	}

	private RegistrarUsuarioAsyncTask registrarTask;
	private DatabaseHelper dbHelper;
	private TextView nombreContacto;
	private EditText txtUserName;
	private Contacto contacto;

	public void btnLoginAceptar_Click(View view) {
		try {
			String userName = this.txtUserName.getText().toString();
			if (userName.length() == 0) {
				Toast.makeText(this.getApplicationContext(), R.string.msjLoginValidate, Toast.LENGTH_LONG).show();
				return;
			}
			this.registrarTask = new RegistrarUsuarioAsyncTask();
			this.registrarTask.execute(userName, "");
			Toast.makeText(this.getApplicationContext(), R.string.userRegistred, Toast.LENGTH_SHORT);
			RegistrarUsuarioActivity.this.finish();
		} catch (Exception ex) {
			Toast.makeText(this.getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
		}
	}

	public void btnLoginCancelar_Click(View view) {
		RegistrarUsuarioActivity.this.finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.registrar_usuario);
		this.txtUserName = (EditText) this.findViewById(R.id.txtUsuario);
		Intent i = this.getIntent();
		// Obtengo el idContacto
		this.contacto = new Contacto();
		this.contacto.setNombre(i.getStringExtra("nombreContacto"));
		this.nombreContacto = (TextView) this.findViewById(R.id.nombreContacto);
		this.nombreContacto.setText(this.contacto.getNombre());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		this.getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
}
